<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page="page" :permission="permissionList"
               :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave"
               @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
      <!--      <template slot-scope="{type,size,row,index}" slot="menuBtn">-->
      <!--        <el-dropdown-item divided @click.native="isStatuClick(row)">是否禁用</el-dropdown-item>-->
      <!--      </template>-->
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>

<!--        <el-button type="info"-->
<!--                   size="small"-->
<!--                   plain-->
<!--                   @click="handleExamine">审核文章-->
<!--        </el-button>-->
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
  school,
  lockorUnlockPlate,
} from "@/api/service/forumContent";
import {mapGetters} from "vuex";
// import iconList from "@/config/iconList";

export default {
  data() {
    return {
      form: {},
      query: {},
      dialogVisible: false,
      isThrottle: false,
      typeRdeio: 1,
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],

      excelBox: false,
      excelForm: {isAnswer: '0'},
      selectData: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "状态",
            prop: "status",
            type: 'select',
            span: 24,
            rules: [{
              required: true,
              message: "请输入反馈内容",
              trigger: "blur"
            }],
            dicData: [{
              label: '审核通过',
              value: 1
            }, {
              label: '审核退回',
              value: 3
            }],
            value: 1
          },
        ]
      },
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 7,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        labelWidth: 120,
        // menuType: 'menu',
        // menuBtnTitle: '更多操作',
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "学校名称",
            prop: "showXxId",//schoolId
            search: true,
            cascader: ['plateId'],
            rules: [{
              required: true,
              message: "请选择学校名称",
              trigger: "blur"
            }],
            type: "tree",
            dicUrl: "/api/ManageArticle/GetRegisterInfo?type=0",
            props: {
              label: "title",
              value: "id"
            },
            formatter: row => {
              return row.showXxName
            }
          },
          {
            label: "年级",
            prop: "showNjName",
            // search: true,
            // maxlength: 20,
            showWordLimit: true,
            overHidden: true,
            editDisplay: false,
            viewDisplay: true,
            addDisplay: false,
            rules: [{
              required: true,
              message: "请输入标题",
              trigger: "blur"
            }],
          },
          {
            label: "年级",
            prop: "showNjId",
            search: true,
            editDisplay: true,
            viewDisplay: false,
            addDisplay: true,
            hide: true,
            rules: [{
              required: true,
              message: "请选择年级",
              trigger: "blur"
            }],
            type: "tree",
            cascader: ['showBjId'],
            dicUrl: "/api/ManageArticle/GetRegisterInfo?type=1",
            props: {
              label: "title",
              value: "id"
            },
          },
          {
            label: "班级",
            prop: "showBjName",
            // search: true,
            // maxlength: 20,
            showWordLimit: true,
            overHidden: true,
            editDisplay: false,
            viewDisplay: true,
            addDisplay: false,
            rules: [{
              required: true,
              message: "请输入班级",
              trigger: "blur"
            }],
          },
          {
            label: "班级",
            prop: "showBjId",
            search: true,
            rules: [{
              required: true,
              message: "请选择班级",
              trigger: "blur"
            }],
            editDisplay: true,
            viewDisplay: false,
            addDisplay: true,
            hide: true,
            type: "tree",
            dicUrl: "/api/ManageArticle/GetRegisterInfo?type=2&njId={{showNjId}}",
            props: {
              label: "title",
              value: "id"
            },
          },
          {
            label: "板块",
            prop: "plateName",
            // search: true,
            // maxlength: 20,
            showWordLimit: true,
            overHidden: true,
            editDisplay: false,
            viewDisplay: true,
            addDisplay: false,
            rules: [{
              required: true,
              message: "请选择板块",
              trigger: "blur"
            }],
          },
          {
            label: "板块",
            prop: "plateId",
            search: true,
            rules: [{
              required: true,
              message: "请选择板块",
              trigger: "blur"
            }],
            editDisplay: true,
            viewDisplay: false,
            addDisplay: true,
            hide: true,
            type: "tree",
            dicUrl: "/api/ManageArticle/GetPlateSelect?schoolId={{key}}",
            props: {
              label: "name",
              value: "id"
            },
          },
          {
            label: "标题",
            prop: "title",
            // search: true,
            // maxlength: 20,
            showWordLimit: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入标题",
              trigger: "blur"
            }],
          },
          {
            label: "关键字",
            prop: "key",
            search: true,
            // maxlength: 20,
            showWordLimit: true,
            overHidden: true,
            hide: true,
            editDisplay: false,
            viewDisplay: false,
            addDisplay: false,
            rules: [{
              // required: true,
              message: "搜索关键字(标题,简介,内容)",
              trigger: "blur"
            }],
            tip:'搜索关键字(标题,简介,内容)',
          },
          {
            label: "封面规则",
            prop: "coverRule",
            type: 'select',
            overHidden: true,
            hide: true,
            rules: [{
              required: true,
              message: "请选择是否需要审核",
              trigger: "blur"
            }],
            dicData: [{
              label: '纯文本',
              value: 0
            }, {
              label: '小单图',
              value: 1
            }],
            value: 0,
            change: ({value}) => {
              const coverUrl1 = this.findObject(this.option.column, "coverUrl1");
              const coverUrl2 = this.findObject(this.option.column, "coverUrl2");
              const coverUrl3 = this.findObject(this.option.column, "coverUrl3");
              school.hide = true;
              if (value === 0) {
                //封面图1
                coverUrl1.editDisplay = false
                coverUrl1.viewDisplay = false
                coverUrl1.addDisplay = false
                coverUrl1.rules[0].required = false
                //封面图2
                coverUrl2.editDisplay = false
                coverUrl2.viewDisplay = false
                coverUrl2.addDisplay = false
                coverUrl2.rules[0].required = false
                //封面图3
                coverUrl3.editDisplay = false
                coverUrl3.viewDisplay = false
                coverUrl3.addDisplay = false
                coverUrl3.rules[0].required = false
              } else if (value === 1) {
                //封面图1
                coverUrl1.editDisplay = true
                coverUrl1.viewDisplay = true
                coverUrl1.addDisplay = true
                coverUrl1.rules[0].required = true
                //封面图2
                coverUrl2.editDisplay = true
                coverUrl2.viewDisplay = true
                coverUrl2.addDisplay = true
                //封面图3
                coverUrl3.editDisplay = true
                coverUrl3.viewDisplay = true
                coverUrl3.addDisplay = true
              }
              this.form.coverRule = value
            }
          },
          {
            label: "封面1",
            prop: "coverUrl1",
            rules: [{
              required: false,
              message: "请选择封面1",
              trigger: "blur"
            }],
            editDisplay: false,
            viewDisplay: false,
            addDisplay: false,
            accept: "image/*",
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            listType: 'picture-img',
            span: 12,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },
          {
            label: "封面2",
            prop: "coverUrl2",
            rules: [{
              required: false,
              message: "请选择封面2",
              trigger: "blur"
            }],
            editDisplay: false,
            viewDisplay: false,
            addDisplay: false,
            accept: "image/*",
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            listType: 'picture-img',
            span: 12,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },
          {
            label: "封面3",
            prop: "coverUrl3",
            rules: [{
              required: false,
              message: "请选择封面3",
              trigger: "blur"
            }],
            editDisplay: false,
            viewDisplay: false,
            addDisplay: false,
            accept: "image/*",
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            listType: 'picture-img',
            span: 24,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },
          {
            label: "描述",
            prop: "desc",
            type: "textarea",
            span: 24,
            hide: true,
            showWordLimit: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入描述",
              trigger: "blur"
            }],
          },
          {
            label: "状态",
            prop: "status",
            type: 'select',
            overHidden: true,
            editDisplay: true,
            viewDisplay: false,
            addDisplay: true,
            hide: true,
            rules: [{
              required: true,
              message: "请选择状态",
              trigger: "blur"
            }],
            dicData: [{
              label: '草稿',
              value: 0
            }, {
              label: '正常',
              value: 1
            }],
            value: 0
          },
          {
            label: "状态",
            prop: "newStatus",
            type: 'select',
            overHidden: true,
            editDisplay: false,
            viewDisplay: true,
            addDisplay: false,
            rules: [{
              required: true,
              message: "请选择状态",
              trigger: "blur"
            }],
            dicData: [{
              label: '草稿',
              value: 0
            }, {
              label: '审核通过',
              value: 1
            }, {
              label: '待审核',
              value: 2
            }, {
              label: '审核退回',
              value: 3
            }],
            value: 0
          },
          {
            label: "是否置顶",
            prop: "isTop",
            type: 'select',
            overHidden: true,
            hide: true,
            rules: [{
              required: true,
              message: "请选择是否置顶",
              trigger: "blur"
            }],
            dicData: [{
              label: '置顶',
              value: true
            }, {
              label: '不置顶',
              value: false
            }],
            value: false
          },
          // {
          //   label: "显示级别 ",
          //   prop: "showLevel",
          //   type: 'select',
          //   overHidden: true,
          //   rules: [{
          //     required: true,
          //     message: "请选择是否置顶",
          //     trigger: "blur"
          //   }],
          //   dicData: [{
          //     label: '学校',
          //     value: 1
          //   }, {
          //     label: '年级',
          //     value: 2
          //   }, {
          //     label: '班级',
          //     value: 3
          //   }],
          //   value: 1
          // },
          {
            label: "排序",
            prop: "sort",
            type: "number",
            width: 80,
            rules: [
              {
                required: true,
                message: "请输入排序",
                trigger: "blur"
              }
            ],
            tip: '数字越大越大越靠前'
          },
          {
            label: "发布时间",
            prop: "releaseTime",
            // search: true,
            type: "datetime",
            format: "yyyy-MM-dd HH:mm",
            hide: true,
            valueFormat: "yyyy-MM-dd HH:mm",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择时间",
              trigger: "blur"
            }],
            maxlength: 50,
            showWordLimit: true
          },
          {
            label: "开始时间",
            prop: "startTime",
            search: true,
            type: "datetime",
            format: "yyyy-MM-dd HH:mm",
            hide: true,
            editDisplay: false,
            viewDisplay: false,
            addDisplay: false,
            valueFormat: "yyyy-MM-dd HH:mm",
            overHidden: true,
            rules: [{
              required: false,
              message: "请选择开始时间",
              trigger: "blur"
            }],
            maxlength: 50,
            showWordLimit: true
          },
          {
            label: "结束时间",
            prop: "endTime",
            search: true,
            type: "datetime",
            format: "yyyy-MM-dd HH:mm",
            hide: true,
            editDisplay: false,
            viewDisplay: false,
            addDisplay: false,
            valueFormat: "yyyy-MM-dd HH:mm",
            overHidden: true,
            rules: [{
              required: false,
              message: "请选择结束时间",
              trigger: "blur"
            }],
            maxlength: 50,
            showWordLimit: true
          },
          {
            label: "内容",
            prop: "content",
            span: 24,
            component: "avueUeditor",
            //wangEditor编辑的配置
            customConfig: {
              // lineHeights: ['1', '1.15', '1.6', '2', '2.5', '3']
            },
            //是否启用oss(qiniu/ali)
            oss: '',
            rules: [
              {
                required: true,
                message: "请输入排序",
                trigger: "blur"
              }
            ],
            headers: {},
            data: {},
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            //普通图片上传
            action: '/api/localfiles/uploadPcFile',
            hide: true
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", "JSuserInfo"]),
    permissionList() {
      return {
        addBtn: true,//this.vaildData(this.permission.bj_add, false),
        viewBtn: true,//this.vaildData(this.permission.bj_view, false),
        delBtn: true,//this.vaildData(this.permission.bj_delete, false),
        editBtn: true
      };
    },
    schoolId() {
      return this.JSuserInfo.schoolId
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    // this.getSchool()
  },
  methods: {
    //是否禁用
    isStatuClick(row) {
      let message = row.status === 0 ? '是否确定启用此板块' : '是否确定禁用此板块'
      this.$confirm(message, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return lockorUnlockPlate({id: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    rowSave(row, done, loading) {
      row.id = '0'
      row.coverUrl1 = row.coverUrl1.toString()
      row.coverUrl2 = row.coverUrl2.toString()
      row.coverUrl3 = row.coverUrl3.toString()
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      row.coverUrl1 = row.coverUrl1.toString()
      row.coverUrl2 = row.coverUrl2.toString()
      row.coverUrl3 = row.coverUrl3.toString()
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    //审核文章
    handleExamine() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.excelBox = true
    },
    beforeOpen(done, type) {
      console.log(type)
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        if (data.total > 0) {
          data.records.forEach(item => {
            item.newStatus = item.status
          })
        }
        this.loading = false;
        this.selectionClear();
      });
    },

  }
};
</script>

<style></style>
